<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <!-- 引入 ACE CDN -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript"
        charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"
        charset="utf-8"></script>
    <!-- 引入jquery cdn -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        .container .navbar {
            width: 100%;
            height: 50px;
            background-color: black;
            /* 给⽗级标签设置overflow，取消后续float带来的影响 */
            overflow: hidden;
        }

        .container .navbar a {
            /* 设置a标签是⾏内块元素，允许你设置宽度 */
            display: inline-block;
            /* 设置a标签的宽度,a标签默认⾏内元素，⽆法设置宽度 */
            width: 80px;
            /* 设置字体颜⾊ */
            color: white;
            /* 设置字体的⼤⼩ */
            font-size: large;
            /* 设置⽂字的⾼度和导航栏⼀样的⾼度 */
            line-height: 50px;
            /* 去掉a标签的下划线 */
            text-decoration: none;
            /* 设置a标签中的⽂字居中 */
            text-align: center;
        }

        /* 设置⿏标事件 */
        .container .navbar a:hover {
            background-color: green;
        }

        .container .navbar .login {
            float: right;
        }

        .container .part1 {
            width: 100%;
            height: 600px;
            overflow: hidden;
        }

        .container .part1 .left_desc {
            width: 50%;
            height: 600px;
            float: left;
            overflow: scroll;
        }

        .container .part1 .left_desc h4 {
            padding-top: 10px;
            padding-left: 10px;
        }

        .container .part1 .left_desc pre {
            padding-top: 10px;
            padding-left: 10px;
            font-size: medium;
            font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS',
                sans-serif;
        }

        .container .part1 .right_code {
            width: 50%;
            float: right;
        }

        .container .part1 .right_code .ace_editor {
            height: 600px;
        }

        .container .part2 {
            width: 100%;
            overflow: hidden;
        }

        .container .part2 .result {
            width: 300px;
            float: left;
        }

        .container .part2 .btn-submit {
            width: 120px;
            height: 50px;
            font-size: large;
            float: right;
            background-color: #26bb9c;
            color: #FFF;
            /* 给按钮带上圆⻆ */
            /* border-radius: 1ch; */
            border: 0px;
            margin-top: 10px;
            margin-right: 10px;
        }

        .container .part2 button:hover {
            color: green;
        }
        .container .part2 .result{
            margin-top: 15px;
            margin-left: 15px;
        }
        .container .part2 .result pre{
            font-size: large;
        }
    </style>

</head>

<body>

    <div class="container">
        <!-- 导航栏， 功能不实现-->
        <div class="navbar">
            <a href="/">⾸⻚</a>
            <a href="/all_questions">题库</a>
            <a href="#">竞赛</a>
            <a href="#">讨论</a>
            <a href="#">求职</a>
            <a class="login" href="#">登录</a>
        </div>
        <!-- 左右呈现，题⽬描述和预设代码 -->
        <div class="part1">
            <div class="left_desc">
                <h4><span id="number">{{number}}</span>.{{title}}_{{star}}</h4>
                <pre>{{desc}}</pre>
            </div>
            <div class="right_code">
                <pre id="code" class="ace_editor"><textarea class="ace_textinput">{{pre_code}}</textarea></pre>

            </div>
        </div>
        <!-- 提交并且得到结果，并显⽰ -->
        <div class="part2">
            <div class="result">我是结果</div>
            <button class="btn-submit" onclick="submit()">提交代码</button>
        </div>
    </div>
    <script>
        //初始化对象 
        editor = ace.edit("code");
        //设置⻛格和语⾔（更多⻛格和语⾔，请到github上相应⽬录查看） 
        // 主题⼤全：http://www.manongjc.com/detail/25-cfpdrwkkivkikmk.html 
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");
        // 字体⼤⼩ 
        editor.setFontSize(16);
        // 设置默认制表符的⼤⼩: 
        editor.getSession().setTabSize(4);
        // 设置只读（true时只读，⽤于展⽰代码） 
        editor.setReadOnly(false);
        // 启⽤提⽰菜单
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });
        function submit() {
            <!-- alert("嘿嘿!"); -->
            // 1. 收集当前⻚⾯的有关数据, 1. 题号 2.代码 
            let code = editor.getSession().getValue();
            console.log(code);
            let number = $(".container .part1 .left_desc h4 #number").text();
            /*console.log(number);*/
            let judge_url = "/judge/" + number;
            //console.log(judge_url);
            // 2. 构建json，并通过ajax向后台发起请求 
            $.ajax({
                method: 'Post',  //向后端发起请求
                url: judge_url, //像后端指定的url发送请求
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    'code': code,
                    'input': ''
                }),
                success: function (data) {
                    // 得到成功的结果！
                    //console.log(data);
                    show_result(data);
                }
            });

            // 3. 得到结果，解析并显⽰到 result中  
            function show_result(data) {
                //console.log(data.status);
                //console.log(data.reason);
                // 拿到 result结果的标签
                let result_div = $(".container .part2 .result");
                let status = data.status;
                let reason = data.reason;
                // 清空上一次的result_div结果
                result_div.empty();
                let reason_label = $("<p>", {
                    text: reason
                });
                reason_label.appendTo(result_div);

                if (status == 0) {
                    // 请求是成功的，但是是否通过，还要看样例的通过情况
                    let _stdout = data.stdout;
                    let _stderr = data.stderr;
                    let stdout_label = $("<pre>", {
                        text: _stdout
                    });
                    let stderr_label = $("<pre>", {
                        text: _stderr
                    });
                    stdout_label.appendTo(result_div);
                    stderr_label.appendTo(result_div);
                }
                else {
                    // 编译运行错误, do nothing

                }
            }
        }
    </script>

</body>+

</html>